package com.thomasmoerman.dci.tmc

import org.squeryl.PrimitiveTypeMode._

trait MoneyTransferQueries {
  this: Database with SessionFactory =>
  
  def calculateBalance(accountId: Long): Double = from(moneyTransfers)(mt => where(mt.accountId === accountId) compute(nvl(sum(mt.amount), 0)))
  
  def persist(moneyTransfer: MoneyTransfer): Unit = moneyTransfers.insert(moneyTransfer)
  
  protected[this] def sum[A](e: NumericalExpression[A]) = new UnaryAgregateFloatOp[A](e, "sum")
  
}